package com.sskj.xyft;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.gyf.barlibrary.ImmersionBar;
import com.gyf.barlibrary.ImmersionFragment;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.sskj.base.view.ProgressDialogs;
import com.sskj.xyft.R;

import butterknife.ButterKnife;
import butterknife.Unbinder;

public abstract class BaseFragment extends ImmersionFragment implements BaseView {

    protected View mView;
    public Unbinder unbinder;
    private ProgressDialogs mProgressDialogs;
    private int mRequestCount;

    private boolean isVisible;

    private boolean isResume;

    private boolean isFirst = true;


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mView = inflater.inflate(getLayoutId(), container, false);
        unbinder = ButterKnife.bind(this, mView);
        mProgressDialogs = new ProgressDialogs(getContext());
        return mView;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

    }

    @Override
    public void onResume() {
        super.onResume();
        isResume = true;
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        isVisible = !hidden;
        if (isResume && isVisible && isFirst) {
            lazyLoad();
            isFirst = false;
        }
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        isVisible = true;
        if (isResume && isVisible && isFirst) {
            lazyLoad();
            isFirst = false;
        }
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        if (unbinder != null) {
            unbinder.unbind();
        }
    }

    public abstract int getLayoutId();

    public abstract void initView();

    public abstract void initData();

    public void loadData() {
    }

    public void initListener() {
    }

    public void lazyLoad() {
    }


    @Override
    public int color(int id) {
        return ContextCompat.getColor(getContext(), id);
    }

    @Override
    public Drawable drawable(int id) {
        return ContextCompat.getDrawable(getContext(), id);
    }

    @Override
    public void initRefreshLayout(SmartRefreshLayout refreshLayout, boolean isFooter) {
        refreshLayout.setRefreshHeader(new ClassicsHeader(getContext()));
        if (isFooter) {
            refreshLayout.setRefreshFooter(new ClassicsFooter(getContext()));
        }
    }

    @Override
    public void initRefreshLayout(SmartRefreshLayout refreshLayout) {
        refreshLayout.setRefreshHeader(new ClassicsHeader(getContext()));
        refreshLayout.setRefreshFooter(new ClassicsFooter(getContext()));
    }

    @Override
    public void setText(TextView textView, String text) {
        if (textView != null) {
            textView.setText(text);
        }
    }

    public String getText(TextView textView) {
        if (textView != null) {
            return textView.getText().toString();
        }
        return "";
    }

    public boolean isEmpty(TextView textView) {
        return textView == null || TextUtils.isEmpty(textView.getText());
    }

    @Override
    public void showLoading() {
        if (mProgressDialogs != null && mRequestCount <= 0) {
            mRequestCount = 1;
            mProgressDialogs.showDialog();
        } else {
            mRequestCount++;
        }
    }

    @Override
    public void hideLoading() {
        if (mRequestCount <= 1) {
            mRequestCount = 0;
            if (mProgressDialogs != null) {
                mProgressDialogs.closeDialog();
            }
        } else {
            mRequestCount--;
        }
    }

    @Override
    public void initImmersionBar() {
        ImmersionBar.with(this).fitsSystemWindows(true).statusBarColor(R.color.colorPrimaryDark).init();
    }

    @Override
    public boolean immersionBarEnabled() {
        return false;
    }
}
